package com.dkm.orproject.dkmoa.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaNews;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.service.OaNewsService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.FileTools;
import com.dkm.orproject.utils.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @PROJECT_NAME: orproject
 * @DESCRIPTION: 新闻的控制层
 * @USER: 周佳佳
 * @DATE: 2020/8/4 16:38
 */
@RestController
@RequestMapping("OaNewsAction")
public class OaNewsAction {
    @Autowired
    OaNewsService oaNewsService;
    @Resource
    Tools tools;
    @Autowired
    FileTools fileTools;
    /**
     * 分页查询
     * @param oaNews 新闻模型用于条件查询
     * @param page 当前页数
     * @param limit 显示行数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 查询结果
     */
    @UserLoginToken
    @GetMapping("selectAll")
    public Map<String,Object> findAll(OaNews oaNews, Integer page, Integer limit, String startTime, String endTime, HttpServletRequest request){
        try {
            //查询总行数
            Long rowCount=oaNewsService.rowCount(oaNews,startTime,endTime);
            //分页查询集合
            List<OaNews> logs=oaNewsService.selectAll(oaNews,startTime,endTime,page,limit);
            //返回集合
            return Tools.maps(logs,rowCount);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("查询新闻的所有方法");
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 新增新闻
     * @param oaNews 新闻模型
     * @param request 服务器请求
     * @return 回调函数
     */
    @UserLoginToken
    @PostMapping("addOrUpdateNews")
    public Map<String,Object> addOrUpdateNews(OaNews oaNews, HttpServletRequest request){

        OaPersonnel user = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        if(ObjectUtils.isEmpty(user)){
            tools.rowMap(map,0,null);
        }
        try {
            if(oaNews.getFileOne().getOriginalFilename()!=null && !"".equals(oaNews.getFileOne().getOriginalFilename())){
                String path = fileTools.saveFile("news", oaNews.getFileOne());
                oaNews.setNewsFile(path);
            }
            if(oaNews.getFileTwo().getOriginalFilename()!=null && !"".equals(oaNews.getFileTwo().getOriginalFilename())){
                String path = fileTools.saveFiletwo("news", oaNews.getFileTwo());
                oaNews.setNewsImg(path);
            }
            int rows=0;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if(oaNews.getPkNewsId()!=null &&!"".equals(oaNews.getPkNewsId())){
                //传入更新人
                oaNews.setUpdatedBy(user.getPersonnelName());
                //传入更新时间
                oaNews.setUpdatedTime(new Date());
                log.setLogTitle("新闻修改模块");
                rows = oaNewsService.updateByPrimaryKeySelective(oaNews);
            }else{
                oaNews.setCreatedBy(user.getPersonnelName());
                oaNews.setIsRevision("1");
                log.setLogTitle("新闻增加模块");
                //新闻类型id为随机生成
                rows = oaNewsService.insertSelective(oaNews);
            }
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("新闻增加或修改模块异常");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
            return map;
        }
    }

    /**
     * 根据id查询出新闻对象
     * @param pkNewsId 新闻id
     * @param session
     * @param request
     * @return 返回一个新闻类型对象
     */
    @GetMapping("findById/{pkNewsId}")
    @UserLoginToken
    public Map<String, Object> selectById(@PathVariable("pkNewsId") Long pkNewsId, HttpSession session, HttpServletRequest request) {
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        try {
            OaNews oaNews = oaNewsService.selectByPrimaryKey(pkNewsId);
            OaLog log = new OaLog();
            log.setLogTitle("新闻根据id查询模块查询成功");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            log.setLogBody(JSON.toJSON(user).toString());
            tools.rowMap(map, 1, log);
            map.put("data",oaNews);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("新闻根据id查询模块异常");
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 删除控制器
     * @param pkNewsId 新闻主键
     * @param session 登录人信息
     * @param request 请求
     * @return 返回结果
     */
    @UserLoginToken
    @DeleteMapping("deleteNews/{pkNewsId}")
    public Map<String, Object> deleteNews(@PathVariable("pkNewsId") Long pkNewsId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            OaLog log = new OaLog();
            OaNews oaNewsType=new OaNews();
            oaNewsType.setPkNewsId(pkNewsId);
            oaNewsType.setIsRevision("0");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogTitle("新闻的删除模块");
            int  rows= oaNewsService.updateByPrimaryKeySelective(oaNewsType);
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            log.setLogBody(JSON.toJSON(user).toString());
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("新闻的删除模块异常");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            tools.errorMap(map,log);
            return map;
        }
    }
    /**
     * 批量删除
     * @param ids 新闻id的集合
     * @param session 操作人的session
     * @param request 请求
     * @return 返回结果
     */
    @DeleteMapping("deleteAll")
    @UserLoginToken
   public Map<String, Object> deleteAll(@RequestParam(value = "ids[]", required = false) Long[] ids, HttpSession session, HttpServletRequest request) {
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        List<Long> list;
        Map<String,Object> map=new HashMap<>();
        if (ObjectUtils.isEmpty(ids)){
            return tools.rowMap(map,0,null);
        }else {
            list= Arrays.asList(ids);
        }
        try {
            OaLog log = new OaLog();
            int deleteAll = oaNewsService.deleteAll(list);
            log.setLogType("1");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogTitle("新闻批量删除模块");
            log.setCreatedBy(user.getPersonnelName());
            tools.rowMap(map, deleteAll, log);
            map.put("data",deleteAll);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("新闻批量删除模块异常");
            tools.errorMap(map,log);
            return map;
        }
    }
}
